package com.arvato.minipro.service.user;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_do.user.EcUserDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_vo.user.EcUserVo;
import com.arvato.minipro.mapper.user.EcUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
@Transactional
public class EcUserService {
	
	@Autowired
	private EcUserMapper ecUserMapper;

	private Logger logger = LoggerFactory.getLogger(EcUserService.class);
	/**
	 * 根据条件查询多个用户
	 * @param query
	 * @param isTotal 是否查询总数
	 * @return
	 */
	public PageData listUserByQuery(Query query) {
		// 设置分页参数
		PageHelper.startPage(query.getPageNum(), query.getPageSize());
		// 执行查询
		List<EcUserVo> userList = ecUserMapper.listUserByQuery(query);
		// 得到结果
		PageInfo<EcUserVo> pageInfo = new PageInfo<EcUserVo>(userList);
		PageData data = new PageData(pageInfo.getTotal(), pageInfo.getList());
		return data;
	}
	/**
	 * 根据条件 查询一个用户
	 * @param query
	 * @return
	 */
	public EcUserVo getUserByQuery(Query query) {
		EcUserVo user = ecUserMapper.getUserByQuery(query);
		return user;
	}
	/**
	 * 新增一个电商小程序用户
	 * @param user
	 * @return
	 */
	public int insert(EcUserDo user) {
		int row = ecUserMapper.insert(user);
		logger.info("新增用户");
		return row;
	}
	/**
	 * 编辑用户
	 * @param user
	 * @return
	 */
	public int update(EcUserDo user) {
		int row = ecUserMapper.update(user);
		Query query = new Query();
		query.put("userId", user.getUserId());
		return row;
	}
	
	/**
	 * 删除用户
	 * @param user
	 * @return
	 */
	public int delete(EcUserDo user) {
		int row = ecUserMapper.update(user);
		return row;
	}
	
	/**
	 * 根据ID查询用户
	 * @param valueOf
	 * @return
	 */
	public EcUserVo getUserById(String userId) {
		EcUserVo user = ecUserMapper.getUserById(userId);
		return user;
	}
	
}
